// tabBarComponent/tabBar.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabbar: {
      type: Object,
      value: {
        "backgroundColor": "#ffffff",
        "color": "#979795",
        "selectedColor": "#1c1c1b",
        "list": [
          {
            "pagePath": "/pages/index/index",
            "iconPath": "icon/icon_home.png",
            "selectedIconPath": "icon/icon_home_HL.png",
            "text": "首页"
          },
          {
            "pagePath": "/pages/growUp/growUp",
            "iconPath": "icon/icon_release.png",
            "selectedIconPath": "icon/icon_release_HL.png",
            "text": "成长"
          },
          {
            "pagePath": "/pages/user/user",
            "iconPath": "icon/icon_mine.png",
            "selectedIconPath": "icon/icon_mine_HL.png",
            "text": "我的"
          }
        ]
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isLogin: true,
    canIUseGetUserProfile: app.globalData.canIUseGetUserProfile
    // isIphoneX: app.globalData.systemInfo.model.search('iPhone X') != -1 ? true : false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    switchTabTo: function(e) {
      console.log(e.currentTarget.dataset.item.pagePath)
      let path = e.currentTarget.dataset.item.pagePath
      this.setData({
        pagePath: e.currentTarget.dataset.item.pagePath
      })
      if(path == '/pages/user/user'){
        if(!app.globalData.userInfo){
          this.setData({
            isLogin: false
          })
          return
        }
      }
      wx.switchTab({
        url: e.currentTarget.dataset.item.pagePath,
      })
    },
    closeLogin: function () {
      this.setData({
        isLogin: true
      })
    },
    bindGetUserInfo (e) {
      if(app.globalData.canIUseGetUserProfile){
        console.log("app.globalData.canIUseGetUserProfile:",app.globalData.canIUseGetUserProfile)
        wx.getUserProfile({
          desc: '用于完善会员资料',
          success: res => {
            console.log('新方法获取用户信息：',res)
            this.setData({
              isLogin: true
            })
            //静默授权
            app.getMemberId({encryptedData:res.encryptedData,iv:res.iv},this.switchTabCb.bind(this))
          }
        })
      }else{
        wx.getUserInfo({
          success: res => {
            // 可以将 res 发送给后台解码出 unionId
            //静默授权
            app.getMemberId({encryptedData:res.encryptedData,iv:res.iv},this.switchTabCb.bind(this))
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if (app.userInfoReadyCallback) {
              app.userInfoReadyCallback(res)
            }
          },
        })
        if(res.userInfo.nickName&&res.userInfo.avatarUrl){
          this.setData({
            isLogin: true
          })
        }
      }
    },
    switchTabCb: function () {
      if(this.data.pagePath){
        wx.switchTab({
          url: this.data.pagePath,
        })
      }
    }
  }
})
